package au.com.iglootech.TaxTimeToolTip.ui.fillupform;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.vaadin.dialogs.ConfirmDialog;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.Task;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;
import au.com.iglootech.TaxTimeToolTip.service.TaskService;
import au.com.iglootech.TaxTimeToolTip.ui.invoice.PerviewWindow;

import com.google.appengine.api.datastore.Key;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

@Configurable(preConstruction = true)
@Component
@Scope("request")
public class EditTimeItemWindow extends Window {

	@Autowired
	private transient TaskService taskService;
	@Autowired
	private transient GenericDao genericDao;
	@Autowired
	private transient CompanyService companyService;

	private Form timeSheetForm = new Form();
	private Task task;

	public EditTimeItemWindow(Key taskKey) {
		super();
		this.task = taskService.findById(taskKey);
		initContent();
	}

	public EditTimeItemWindow(Task task) {
		super();
		this.task = task;
		initContent();
	}

	public void initContent() {
		this.task = task;
		setWidth("500px");
		setHeight("400px");
		setCaption("Edit Task");
		setModal(true);
		Panel p = new Panel();
		// get companty
		Company company = companyService.findById(task.getCompany().getKey());
		task.setCompany(company);
		BeanItem<Task> taskItem = new BeanItem<Task>(task);
		timeSheetForm.setCaption("Edit Task");
		timeSheetForm.setWriteThrough(false); // we want explicit 'apply'
		timeSheetForm.setInvalidCommitted(false); // no invalid values in
													// datamodel

		// FieldFactory for customizing the fields and adding validators
		timeSheetForm.setFormFieldFactory(new TimeSheetFieldsFactory());
		timeSheetForm.setItemDataSource(taskItem); // bind to POJO via BeanItem

		// Determines which properties are shown, and in which order:
		timeSheetForm.setVisibleItemProperties(Arrays.asList(new String[] {
				"taskName", "period", "unit", "startDate",
				"description" }));
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		Button apply = new Button("Apply", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					timeSheetForm.commit();
					taskService.updateTask(task);
					getWindow().showNotification(
							"Your changes have been saved.");
					EditTimeItemWindow.this.close();
				} catch (Exception e) {
					// Ignored, we'll let the Form handle the errors
					System.out.print(e);
				}
			}
		});
		Button cancel = new Button("Cancel", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				EditTimeItemWindow.this.close();
			}
		});
		Button delete = new Button("Delete", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				ConfirmDialog.show(EditTimeItemWindow.this, "Please Confirm:",
						"Are you really sure?", "I am", "Not quite",
						new ConfirmDialog.Listener() {
							public void onClose(ConfirmDialog dialog) {
								if (dialog.isConfirmed()) {
									taskService.remove(task);
									getWindow().showNotification(
											"Your task has been removed.");
									EditTimeItemWindow.this.close();
								} else {
									EditTimeItemWindow.this.close();
								}
							}
						});

			}
		});
		buttons.addComponent(apply);
		buttons.addComponent(cancel);
		buttons.addComponent(delete);
		timeSheetForm.getFooter().addComponent(buttons);
		timeSheetForm.getFooter().setMargin(false, false, true, true);
		p.addComponent(timeSheetForm);
		setContent(p);
	}
}
